package com.woke.fragment;

import java.util.ArrayList;

import com.zhongcai.online.R;

import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
 * 
 * @author Xiongkai
 * @hint 
 *       PagerSlidingTabStripNative�Ĺ����࣬��Ҫʵ��setViewPagerAdapter(LocalPagerAdapter
 *       pagerAdapter)
 */
public abstract class ViewPagerFragment extends Fragment {
	PagerAdapter mAdapter;
	private final Handler mHandler = new Handler();
	private final ViewPager.OnPageChangeListener mOnPagerChangeListener = new ViewPager.OnPageChangeListener() {
		public void onPageScrollStateChanged(int paramInt) {
			ViewPagerFragment.this.onPageScrollStateChanged(paramInt);
		}

		public void onPageScrolled(int paramInt1, float paramFloat, int paramInt2) {
			ViewPagerFragment.this.onPageScrolled(paramInt1, paramFloat, paramInt2);
		}

		public void onPageSelected(int paramInt) {
			ViewPagerFragment.this.onPageSelected(paramInt);
		}
	};
	private final Runnable mRequestFocus = new Runnable() {
		public void run() {
			ViewPagerFragment.this.mViewPager.focusableViewAvailable(ViewPagerFragment.this.mViewPager);
		}
	};
	ViewPager mViewPager;
	PagerSlidingTabStripNative tabStrip;

	public int getSelectedItemPosition() {
		return this.mViewPager.getCurrentItem();
	}

	public ViewPager getViewPager() {
		return this.mViewPager;
	}

	public PagerAdapter getViewPagerAdapter() {
		return this.mAdapter;
	}

	public View onCreateView(LayoutInflater paramLayoutInflater, ViewGroup paramViewGroup, Bundle paramBundle) {
		View localView = paramLayoutInflater.inflate(R.layout.viewpagerfragmentlay, null);
		this.mViewPager = ((ViewPager) localView.findViewById(R.id.viewpager));
		this.tabStrip = ((PagerSlidingTabStripNative) localView.findViewById(R.id.tabs));
		this.tabStrip.setOnPageChangeListener(this.mOnPagerChangeListener);
		return localView;
	}

	public void onDestroyView() {
		super.onDestroyView();
		this.mHandler.removeCallbacks(this.mRequestFocus);
		this.mViewPager = null;

	}

	protected void onPageScrollStateChanged(int position) {
	}

	protected void onPageScrolled(int position, float paramFloat, int paramInt2) {
	}

	/**
	 * �л�tabʱ�������¼�
	 * 
	 * @param position
	 *            �������ڼ���ҳ��
	 */
	protected abstract void onPageSelected(int position);

	/**
	 * 
	 * ViewPager����ҳ�������
	 * 
	 * @param paramInt
	 */
	public void setOffscreenPageLimit(int paramInt) {
		if (paramInt > 1)
			this.mViewPager.setOffscreenPageLimit(paramInt);
	}

	public void setSelection(int paramInt) {
		this.mViewPager.setCurrentItem(paramInt);
	}

	public void setViewPagerAdapter(LocalPagerAdapter pagerAdapter) {
		this.mAdapter = pagerAdapter;
		if (this.mViewPager != null) {
			this.mViewPager.setAdapter(pagerAdapter);
			mViewPager.setOffscreenPageLimit(mAdapter.getCount());
			this.tabStrip.setViewPager(this.mViewPager);
			onPageSelected(0);
		}

	}

	public class LocalPagerAdapter extends FragmentStatePagerAdapter {
		private ArrayList<Fragment> mdatas;
		private String[] mtitles;

		public LocalPagerAdapter(FragmentManager fm, ArrayList<Fragment> fragments, String[] titles) {
			super(fm);
			// TODO Auto-generated constructor stub
			mdatas = fragments;
			mtitles = titles;
		}

		@Override
		public int getCount() {
			if (mdatas == null)
				return 0;
			return mdatas.size();
		}

		@Override
		public Fragment getItem(int paramInt) {
			if (mdatas == null)
				return null;
			return (Fragment) mdatas.get(paramInt);
		}

		@Override
		public CharSequence getPageTitle(int position) {
			// TODO Auto-generated method stub
			return mtitles[position];
		}

	}

}
